package com.example.login.vm

import com.example.common.view.vm.BaseViewModel
import com.example.login.api.UserApiService
import com.example.login.model.LoginEntity
import com.example.login.view.UserRepository
import com.example.login.view.UserResultData
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class LoginViewModel @Inject constructor(private val apiService: UserApiService): BaseViewModel<UserRepository, UserResultData<List<LoginEntity>>>(){
    fun postLogin(password:String,username:String){
        mViewModelScope.launch (mDispatcher){
            try{
                val result=apiService.login(password,username)
                val res= UserResultData(
                    state = result.state,
                    msg =result.msg,
                    data = listOf(result.data)
                )
                mSuccessLiveData.postValue(res)
            }catch (e:Exception){
                mErrorLiveData.postValue("请求失败:${e.message}")
            }
        }
    }
}